/*
 * @(#)TransformArch.java   09/12/09
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



/**
 *
 */
package fr.ema.arch3d.jmodel.transformXML;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * class description
 *
 * @version    version 1 nov. 2009
 * @author     Huaxi (Yulin) ZHANG
 */
public class TransformArch {

    /**
     * Method description
     *
     *
     * @param specUri
     * @param cmpRoleDBUri
     *
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws TransformerException
     */
    public static void transformSpecXml(String specUri, String cmpRoleDBUri)
            throws IOException, SAXException, ParserConfigurationException, TransformerException {

        // get component role names of this specification
        String cmpRoleListQuery = "for $x in Specification/componentRoles " + "return data($x)";
        String cmpRoles         = xQuery.select(specUri, cmpRoleListQuery);

        cmpRoles = cmpRoles.substring(38);

        // get component roles of specification from component role database
        String cmpRoleXML = xQuery.selectCmpRoles(cmpRoleDBUri, cmpRoles);

        // remove componentRoles from specification
        OperateNode.removeNode(specUri, "componentRoles");

        // get specification file
        File specFile = new File(specUri);

        // construct builders
        DocumentBuilderFactory factory  = DocumentBuilderFactory.newInstance();
        DocumentBuilder        builder  = factory.newDocumentBuilder();
        TransformerFactory     tFactory = TransformerFactory.newInstance();
        Transformer            tFormer  = tFactory.newTransformer();

        // parse two xml files
        Document docCmpRole  = builder.parse(new InputSource(new StringReader(cmpRoleXML)));
        Document docSpec     = builder.parse(specFile);
        NodeList cmpRoleList = docCmpRole.getElementsByTagName("componentRole");
        Node     n;

        for (int i = 0; i < cmpRoleList.getLength(); i++) {
            n = docSpec.importNode(cmpRoleList.item(i), true);
            docSpec.getElementsByTagName("Specification").item(0).appendChild(n);
        }

        // merge two files: merge component roles to the specification
        docSpec.normalize();

        Source source = new DOMSource(docSpec);
        Result result = new StreamResult(specFile);

        tFormer.transform(source, result);
    }

    /**
     * Method description
     *
     *
     * @param specUri
     *
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws TransformerException
     */
    public static void transformSpecXml(String specUri)
            throws IOException, SAXException, ParserConfigurationException, TransformerException {
        String cmpRoleDBUri =
            "E:/Yulin/Nime/Code/workspace/Arch3D-Model/src/main/resources/fr/ema/arch3d/database/componentRoleDB.xml";

        transformSpecXml(specUri, cmpRoleDBUri);
    }
}
